package com.itwmk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwmk.entity.Category;
import com.itwmk.service.impl.CategoryService;
import com.itwmk.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wmk
 */
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 增加菜品功能
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addType(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }
    /**
     * 查询菜品信息功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> queryCategory(int page,int pageSize){
        Page<Category> categoryPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(categoryPage,wrapper);
        return R.success(categoryPage);

    }
    /**
     * 删除菜品分类功能
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(Long ids){
        categoryService.myRemoveById(ids);
        return R.success("删除成功");
    }

    /**
     * 修改菜品种类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据菜品类型查询菜品种类
     * @param category
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> getCategory(Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getName);
        List<Category> categoryList = categoryService.list(wrapper);
        return R.success(categoryList);
    }
}
